package 链表;

import 队列.数组队列.Queue;
/*尾指针链表队列*/
public class LinkedListQueue<E> implements Queue<E> {
    private class Node{
        public Node next;
        public E e ;
        public Node(Node next, E e) {
            this.next = next;
            this.e = e;
        }
        public Node(E e ) {
            this(null, e);
        }
        public Node() {
            this(null, null);
        }
        @Override
        public String toString() {
            return e.toString();
        }
    }
    private Node head, tail;
    private int size;

    public LinkedListQueue() {
        head = null;
        tail = new Node();
        size = 0;
    }
    @Override
    public void enqueue(E e) {
        if (head == null){
            head = new Node(e);
            tail = head;
        }
        Node node = new Node(e);
        tail.next = node;
        tail = tail.next;
        size++;
    }

    @Override
    public E dequeue() {
        if (isEmpty()){
            throw new IllegalArgumentException("error");
        }
        Node deleteNode = head;
        head = head.next;
        if (head == null){
            tail = null;
        }
        deleteNode.next = null;
        size--;
        /**/
        return deleteNode.e;
    }

    @Override
    public E getFront() {
        if (isEmpty()){
            throw new IllegalArgumentException("error");
        }
        return head.e;
    }

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        Node cur = head.next;
        res.append("front: ");
        while (cur != null){
            res.append(cur).append("->");
            cur = cur.next;
        }
        res.append("null tail");
        return res.toString();
    }

    public static void main(String[] args) {
        LinkedListQueue<Integer> queue = new LinkedListQueue<>();
        for (int i = 0; i < 10; i++) {
            queue.enqueue(i);
            System.out.println(queue);
                queue.dequeue();
                System.out.println(queue);

        }



    }
}
